﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;

namespace WpfExceptionWindow
{
    class ExceptionTemplateSelector : DataTemplateSelector
    {
        public DataTemplate RootTemplate { get; set; }
        public DataTemplate InnerTemplate { get; set; }
        public DataTemplate LastItemTemplate { get; set; }

        private static int ExceptionCount = -1;

        public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container)
        {
            var exception = item as Exception;
            if (exception == null)
                return null;

            ExceptionCount++;

            if (ExceptionCount > 0)
            {
                if (exception.InnerException == null)
                    return LastItemTemplate;
                else
                    return InnerTemplate;
            }
            else
                return RootTemplate;
        }
    }
}
